def search_2d(matrix, num):
    m = len(matrix)
    n = len(matrix[0])
    i = 0
    j = n-1
    while i>=0 and i<m and j>=0 and j<n:
        if matrix[i][j] == num:
            return True
        if matrix[i][j] > num:
            j-=1
        else:
            i+=1
    return False

print(search_2d([
  [1,   4,  7, 11, 15],
  [2,   5,  8, 12, 19],
  [3,   6,  9, 16, 22],
  [10, 13, 14, 17, 24],
  [18, 21, 23, 26, 30]
], 5))
print(search_2d([
  [1,   4,  7, 11, 15],
  [2,   5,  8, 12, 19],
  [3,   6,  9, 16, 22],
  [10, 13, 14, 17, 24],
  [18, 21, 23, 26, 30]
], 20))